package com.xmad.ksadutils.AdSDK.AdMold.OnCallback;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;


import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.xmad.ksadutils.AdSDK.Interface.AdOnLoad;

public class KsLoadSplashScreenAd implements AdOnLoad {
    KsSplashScreenAd ksSplashScreenAd;
    @Override
    public void noCallback(Long id, Activity activity, FrameLayout frameLayout) {
        KsScene scene = new KsScene.Builder(id)
                //是否需要开屏小窗展示，默认为false, 设置false后将不会回调 onShowMiniWindow
                .needShowMiniWindow(true)
                .build(); // 此为测试posId，请联系快手平台申请正式posId
        KsAdSDK.getLoadManager().loadSplashScreenAd(scene, new KsLoadManager.SplashScreenAdListener() {
            @Override
            public void onError(int code, String msg) {
                Log.e("onError",code+"========="+msg);
            }
            @Override
            public void onRequestResult(int adNumber) {

            }
            @Override
            public void onSplashScreenAdLoad(@NonNull KsSplashScreenAd splashScreenAd) {
                // addView(splashScreenAd,frameLayout);
                ksSplashScreenAd = splashScreenAd;
                View view = splashScreenAd.getView(activity, new KsSplashScreenAd.SplashScreenAdInteractionListener() {
                    @Override
                    public void onAdClicked() {
                        Log.e("onAdClicked","onAdClicked");
                    }

                    @Override
                    public void onAdShowError(int code, String extra) {
                        ksSplashScreenAd = null;
                    }

                    @Override
                    public void onAdShowEnd() {
                        Log.e("onAdShowEnd","onAdShowEnd");
                        frameLayout.removeAllViews();
                    }

                    @Override
                    public void onAdShowStart() {
                    }

                    @Override
                    public void onSkippedAd() {
                        Log.e("onSkippedAd","onSkippedAd");
                    }

                    @Override
                    public void onDownloadTipsDialogShow() {

                    }

                    @Override
                    public void onDownloadTipsDialogDismiss() {

                    }

                    @Override
                    public void onDownloadTipsDialogCancel() {

                    }
                });

                if (!activity.isFinishing()) {
                    //ViewGroup root = activity.findViewById(R.id.frameLayout);
                    //root.removeAllViews();
                    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    frameLayout.addView(view);
                }
            }
        });
    }

    @Override
    public void noCallback(String id, Activity activity, FrameLayout frameLayout) {

    }
}
